package com.funtl.framework.web.action.purview;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.springframework.context.annotation.Scope;

import com.funtl.framework.java.abstracts.AbstractAction;
import com.funtl.framework.java.common.util.bean.DateUtil;
import com.funtl.framework.web.entity.Admin;
import com.funtl.framework.web.entity.PurviewColumn;
import com.funtl.framework.web.entity.PurviewMenu;
import com.funtl.framework.web.service.purview.IPurviewColumnService;
import com.funtl.framework.web.service.purview.IPurviewMenuService;

/**
 * 权限菜单
 * 
 * @author WMLI Create on 2013-5-14
 */
@Scope("prototype")
public class PurviewMenuAction extends AbstractAction {
	private static final long serialVersionUID = -914503161065616011L;
	private JSONObject result;
	private Map<String, Object> request;
	private Map<String, Object> session;
	
	private PurviewMenu purviewMenu;
	private IPurviewMenuService purviewMenuService;
	private IPurviewColumnService purviewColumnService;
	
	// page

	public String menu_manager() throws Exception {
		//加载权限栏目
    	List<PurviewColumn> purviewColumns = purviewColumnService.getList(new PurviewColumn());
    	request.put("purviewColumns", purviewColumns);
    	
    	return SUCCESS;
	}

	// common action

	@Override
	public String get() throws Exception {
		return null;
	}

	@Override
	public String query() throws Exception {
		//加载权限栏目
    	List<PurviewColumn> purviewColumns = purviewColumnService.getList(new PurviewColumn());
    	List<PurviewMenu> purviewMenus = purviewMenuService.getList(new PurviewMenu());
    	List<PurviewMenu> list = new ArrayList<PurviewMenu>();
    	
    	PurviewColumn purviewColumn = null;
    	PurviewMenu purviewMenu = null;
    	
    	for (int x = 0 ; x < purviewColumns.size() ; x++) {
    		purviewColumn = purviewColumns.get(x);
    		for (int y = 0 ; y < purviewMenus.size() ; y++) {
    			purviewMenu = purviewMenus.get(y);
    			if (purviewColumn.getId().equals(purviewMenu.getPurviewColumnId())) {
    				purviewMenu.setParams(purviewColumn.getColumnName());
    				list.add(purviewMenu);
    			}
    		}
    	}
    	
    	jsonData.put("rows", list);
		result = JSONObject.fromObject(jsonData);
		return SUCCESS;
	}

	@Override
	public String insert() throws Exception {
		Admin admin = (Admin) session.get("admin");
		purviewMenu.setCreateDate(DateUtil.getCurrentDate());
		purviewMenu.setCreateId(admin.getId());
		purviewMenu.setCreateName(admin.getUserName());
		purviewMenuService.insert(purviewMenu);
		result = JSONObject.fromObject(jsonData);
		return SUCCESS;
	}

	@Override
	public String delete() throws Exception {
		purviewMenuService.delete(purviewMenu);
		result = JSONObject.fromObject(jsonData);
		return SUCCESS;
	}

	@Override
	public String update() throws Exception {
		Admin admin = (Admin) session.get("admin");
		purviewMenu.setUpdateDate(DateUtil.getCurrentDate());
		purviewMenu.setUpdateId(admin.getId());
		purviewMenu.setUpdateName(admin.getUserName());
		purviewMenuService.update(purviewMenu);
		result = JSONObject.fromObject(jsonData);
		return SUCCESS;
	}

	// getter setter

	public JSONObject getResult() {
		return result;
	}

	public void setResult(JSONObject result) {
		this.result = result;
	}

	public Map<String, Object> getRequest() {
		return request;
	}

	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public PurviewMenu getPurviewMenu() {
		return purviewMenu;
	}

	public void setPurviewMenu(PurviewMenu purviewMenu) {
		this.purviewMenu = purviewMenu;
	}

	public IPurviewMenuService getPurviewMenuService() {
		return purviewMenuService;
	}

	@Resource
	public void setPurviewMenuService(IPurviewMenuService purviewMenuService) {
		this.purviewMenuService = purviewMenuService;
	}
	
	public IPurviewColumnService getPurviewColumnService() {
		return purviewColumnService;
	}

	@Resource
	public void setPurviewColumnService(IPurviewColumnService purviewColumnService) {
		this.purviewColumnService = purviewColumnService;
	}
	
}
